import { submitFeedback, uploadFeedbackImg, wxChooseImage } from '../../api/feedback/feedback';
import { timeoutTask, showWxToast, showWxLoading, hideWxLoading } from '../../utils/index';
// 投诉页面需要传userId
let btnLock = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    txtVal: 0,
    typeList: [{
      name: "支付未到账",
      isActive: false
    }, {
      name: "误点击支付",
      isActive: false
    }, {
      name: "不明的扣款",
      isActive: false
    }, {
      name: "红包未到账",
      isActive: false
    }, {
      name: "其他原因",
      isActive: false
    }],
    activeTypeStr: "",
    desc: "",
    phone: "",
    uploaderList: [],
    uploadImgUrlList: [],
    uploaderNum: 0,
    showUpload: true,
    userId: '',
  },

  onLoad: function (options) {
    console.log(options.userId);
    this.setData({
      userId: options.userId,
    });
  },
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  descInput: function (e) {
    this.setData({
      txtVal: e.detail.value.length,
      desc: e.detail.value
    })
    if (this.data.txtVal >= 120) {
      showWxToast('问题内容不能超过120字哦!');
      this.setData({
        desc: this.data.desc.slice(0, 120)
      })
    }
  },
  chooseType(e) {
    if (e.currentTarget.dataset.active) {
      this.resetTypeList();
    } else {
      let tmpCount = this.getTypeListActiveNum();
      if (tmpCount == 0) {
        this.updateTypeList(e.currentTarget.dataset.name);
      } else {
        this.resetTypeList();
        this.updateTypeList(e.currentTarget.dataset.name);
      }
    }
  },
  getTypeListActiveNum() {
    let tmpTypeList = this.data.typeList;
    let tmpCount = 0;
    for (let i = 0; i < tmpTypeList.length; i++) {
      if (tmpTypeList[i].isActive) {
        tmpCount++;
      }
    }
    return tmpCount;
  },
  updateTypeList(name) {
    let tmpTypeList = this.data.typeList;
    for (let i = 0; i < tmpTypeList.length; i++) {
      if (tmpTypeList[i].name == name) {
        tmpTypeList[i].isActive = !tmpTypeList[i].isActive
        break;
      }
    }
    this.setData({
      typeList: tmpTypeList
    })
  },
  resetTypeList() {
    let tmpTypeList = this.data.typeList;
    for (let i = 0; i < tmpTypeList.length; i++) {
      if (tmpTypeList[i].isActive) {
        tmpTypeList[i].isActive = false;
      }
    }
    this.setData({
      typeList: tmpTypeList
    })
  },

  submit() {
    if (btnLock) {
      return;
    }
    this.setData({
      activeTypeStr: ""
    })
    let tmpActiveTypeStr = "";
    let tmpImgUrlStr = "";
    let tmpImgUrlList = this.data.uploadImgUrlList;
    let tmpImgUrlListSub = [];
    let tmpTypeList = this.data.typeList;
    for (let i = 0; i < tmpImgUrlList.length; i++) {
      tmpImgUrlListSub.push(tmpImgUrlList[i].value);
    }
    for (let i = 0; i < tmpTypeList.length; i++) {
      if (tmpTypeList[i].isActive) {
        tmpActiveTypeStr += (tmpTypeList[i].name + " ");
      }
    }
    this.setData({
      activeTypeStr: tmpActiveTypeStr
    })
    if (!this.data.activeTypeStr) {
      showWxToast('至少选择一个问题类型哦!');
      return;
    }
    if (!this.data.desc) {
      showWxToast('请填写问题描述!');
      return;
    }
    if (!this.data.phone) {
      showWxToast('请填写手机号!');
      return;
    }

    if (!(this.data.phone.length == 11)) {
      showWxToast('手机号格式不正确!');
      return;
    }
    if (tmpImgUrlListSub.length > 0) {
      tmpImgUrlStr = tmpImgUrlListSub.join(',');
    }

    console.log(this.data.desc);
    console.log(this.data.phone);
    console.log(this.data.activeTypeStr);
    console.log(tmpImgUrlStr);
    btnLock = true;
    submitFeedback({
      desc: this.data.desc,
      phone: this.data.phone,
      type: this.data.activeTypeStr,
      imgurl: tmpImgUrlStr,
    }).then((res) => {
      console.log(res);
      showWxToast('投诉信息提交成功');
      timeoutTask(() => {
        btnLock = false;
        wx.navigateBack();
      }, 1000);
    }).catch((err) => {
      console.log(err);
      btnLock = false;
      showWxToast('投诉信息提交失败');
    });
  },
  // 删除图片
  clearImg: function (e) {
    var nowList = []; //新数据

    var uploaderList = this.data.uploaderList; //原数据
    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        this.deleteImgUrlByKey(uploaderList[i])
        continue;
      } else {
        nowList.push(uploaderList[i])
        continue;
      }
    }
    this.setData({
      uploaderNum: this.data.uploaderNum - 1,
      uploaderList: nowList,
      showUpload: true
    })
  },
  deleteImgUrlByKey(key) {
    let tmpImgUrlList = this.data.uploadImgUrlList;
    for (let i = 0; i < tmpImgUrlList.length; i++) {
      if (tmpImgUrlList[i].key == key) {
        tmpImgUrlList.splice(i, 1);
        break;
      }
    }
    this.setData({
      uploadImgUrlList: tmpImgUrlList
    })
  },
  //展示图片
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList,
      current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
  },
  //上传图片
  upload: function (e) {
    let tempFilePaths;
    wxChooseImage(this.data.uploaderNum).then((res) => {
      console.log(res);
      showWxLoading('上传图片中...');
      tempFilePaths = res.tempFilePaths;
      return uploadFeedbackImg(tempFilePaths[0]);
    }).then((res2) => {
      hideWxLoading();
      console.log(res2);
      let tmpRes = JSON.parse(res2.data);
      console.log(tmpRes.data.filePath);
      let tmpImgUrlList = this.data.uploadImgUrlList;
      let uploaderList = this.data.uploaderList.concat(tempFilePaths);
      tmpImgUrlList.push({
        key: tempFilePaths[0],
        value: tmpRes.data.filePath
      })
      if (uploaderList.length == 3) {
        this.setData({
          showUpload: false
        });
      }
      this.setData({
        uploaderList: uploaderList,
        uploaderNum: uploaderList.length,
        uploadImgUrlList: tmpImgUrlList
      })
    }).catch((err) => {
      console.log(err);
      hideWxLoading();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }

})